package jok.serial;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import jok.io.FileDemo;

public class ObjectOutputStreamDemo {

	public static void main(String[] args) {

		writeObj();

		FileDemo.deleteFile("D:/a.txt");
	}

	public static void writeObj() {
		Student stu = new Student(1, "张三", 18);
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("D:/a.txt"));
			oos.writeObject(stu);
			oos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (oos != null)
					oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeObjList() {
		List<Student> list = new ArrayList<Student>();
		list.add(new Student(1, "张三", 18));
		list.add(new Student(2, "李四", 26));
		list.add(new Student(3, "王五", 22));

		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("D:/a.txt"));
			oos.writeObject(list);
			oos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (oos != null)
					oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
